/**
 * ssk-common#com.anxin.ssk.common.download#Downloader.java
 */
package com.firefly.app.biz.download;

import java.util.concurrent.LinkedBlockingQueue;


/**
 * @author: (le.qiao)
 * @e-mail: qiaolevip@gmail.com
 * @myblog: <a href="http://qiaolevip.iteye.com">http://qiaolevip.iteye.com</a>
 * @date: 2013-12-26
 * 
 */
public class Downloader {
	
	

	public boolean download(String url, String targetPath) {
		boolean v = false;
		try {
			LinkedBlockingQueue<String> downloadQueue = new LinkedBlockingQueue<String>();
			SiteInfoBean bean = new SiteInfoBean(url, targetPath, getFileNameFromUrl(url), 1);
			SiteFileFetch fileFetch = new SiteFileFetch(bean,downloadQueue);
			fileFetch.start();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}

	private String getFileNameFromUrl(String url) {
		String fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
		Utility.log("fileName=" + fileName);
		return fileName;
	}

	public boolean download(String url, String targetPath, String filename) {
		boolean v = false;
		try {
			LinkedBlockingQueue<String> downloadQueue = new LinkedBlockingQueue<String>();
			SiteInfoBean bean = new SiteInfoBean(url, targetPath, filename, 2);
			SiteFileFetch fileFetch = new SiteFileFetch(bean,downloadQueue);
			fileFetch.start();
			
			String downLoad = downloadQueue.take();
			Utility.log("下载完成=" + downLoad);
			if ("true".equals(downLoad)) {
				Utility.log("下载程序返回成功=" + downLoad);
				v = true;
			}else {
				Utility.log("下载程序返回失败=" + downLoad);
				v = false;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
}
